package com.gzc.pm.ro;

import java.util.List;

import cn.skyclass.net.OperationResult;

import com.gzc.pm.bean.CPLAxleBean;
import com.gzc.pm.bean.CPLLineBean;
import com.gzc.pm.dao.CPLAxleDao;
import com.gzc.pm.dao.CPLLineDao;
import com.gzc.pm.dao.CPLRouteDao;

public class CPLAxleRo {

	public List<CPLAxleBean> getAll(){
		CPLAxleDao dao = new CPLAxleDao();
		return dao.getAll();
	}
	
	public OperationResult addAxle(String name, List<CPLLineBean> lines){
		CPLAxleDao axleDao = new CPLAxleDao();
		CPLLineDao lineDao = new CPLLineDao();
		
		CPLAxleBean axle = new CPLAxleBean();
		axle.setName(name);
		OperationResult result = axleDao.addOne(axle);
		if(result.isSuccessful()){
			int axleId = result.getId();
			for(CPLLineBean line : lines){
				line.setAxleId(axleId);
			}
			result = lineDao.addLines(lines);
			result.setId(axleId);
		}
		
		return result;
	}
	
	public OperationResult delAxle(int axleId){
		CPLAxleDao axleDao = new CPLAxleDao();
		CPLLineDao lineDao = new CPLLineDao();
		CPLRouteDao routeDao = new CPLRouteDao();
		
		OperationResult result = axleDao.deleteById(axleId);
		if(result.isSuccessful()){
			lineDao.deleteByAxleId(axleId);
			routeDao.deleteByAxleId(axleId);
		}
		return result;
	}
	
}
